<?php

namespace App\Http\Controllers\Admin\Sys;

use App\Http\Models\Admin\CMS\Category;
use App\Http\Models\Admin\File\FileContent;
use App\Http\Models\Admin\File\FileContentHistory;
use App\Http\Responses\ResponseDto;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use Psy\Util\Json;


class FilecontentsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //
        //
        return View::make('admin.sys.filecontents')
            ->withUrltitle(trans("admin/message.filecontents_index"))
            ->withTemplates(Config::get("common.templates"))
            ->withAssets($request['ASSETS']);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getTreeData()
    {
        //
//        var_dump("getTreeData");
        $list = FileContent::select('id','name','title','parent_id')
            ->whereNull('parent_id')->orWhere('parent_id',0)->orderBy('show_index','asc')->get();
        foreach($list as $item){
            $this->inTreeData($item);
        }
        return Json::encode($list);
    }

    public function inTreeData($data)
    {
        //
        $data->children =   FileContent::select('id','name','title','parent_id')->where('parent_id',$data->id)->orderBy('show_index','asc')->get();

        if($data->children == null){
            return;
        }
        foreach($data->children as $child){
            $this->inTreeData($child);
        }

    }
    public function updateCtrees(Request $request)
    {
        $data_src = $request->input("data");
        $datas = json_decode($data_src);
        FileContent::whereNotNull('id')->update(['parent_id'=>0]);

        foreach($datas as $k=>$data){
            FileContent::where('id', $data->id)->update(['parent_id'=>0,'show_index'=>$k]);
            $this->updateCtreesItem($data);
        }
        return ResponseDto::createResObj(trans("admin/message.update_success"),ResponseDto::$SUCCESS);
    }
    public function updateCtreesItem($item)
    {
        if(property_exists($item,'children')) {
            foreach($item->children as $k=>$citem){
                FileContent::where('id', $citem->id)->update(['parent_id'=>$item->id,'show_index'=>$k]);
                $this->updateCtreesItem($citem);
            }
        }
    }
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */

    public function store(Request $request)
    {
        $id = $request->input("id");

            $validator = Validator::make(Input::all(),
            [
                'title' => 'required',
                'name' => 'required',
            ]);



        if($validator->fails()){
            return ResponseDto::createResObj($validator->errors()->first(),ResponseDto::$FAIL);
        }else{



            $filecontent = new FileContent();
            $file_history = new FileContentHistory();
            $file_history->name = $filecontent->name = $request->input("name");
            $file_history->title = $filecontent->title = $request->input("title");
            $file_history->template = $filecontent->template = $request->input("template");
            $file_history->is_show = $filecontent->is_show = $request->input("is_show");
            $file_history->page_content = $filecontent->page_content = $request->input("page_content");


            $file_history->use_seo = $filecontent->use_seo = $request->input("use_seo");
            $file_history->seo_title = $filecontent->seo_title = $request->input("seo_title");
            $file_history->seo_keywords = $filecontent->seo_keywords = $request->input("seo_keywords");
            $file_history->seo_description = $filecontent->seo_description = $request->input("seo_description");
            $file_history->seo_meta = $filecontent->seo_meta = $request->input("seo_meta");



            if("" !== $request->input("parent_id")){
                $file_history->parent_id = $filecontent->parent_id = $request->input("parent_id");
            }

            if($request->input("id")){
                //更新操作
                $exit_item = FileContent::where('id','<>',$request->input("id"))->where('name',$filecontent->name)->first();
                if($exit_item){
                    return ResponseDto::createResObj(trans("admin/message.name_exit"),ResponseDto::$FAIL);
                }
                else{
                    $up_item = FileContent::find($request->input("id"));
                    $up_item->name = $filecontent->name;
                    $up_item->title = $filecontent->title;
                    $up_item->page_content = $filecontent->page_content;
                    $up_item->parent_id = $filecontent->parent_id;
                    $up_item->template = $filecontent->template;
                    $up_item->is_show = $filecontent->is_show;
                    $up_item->use_seo = $filecontent->use_seo;
                    $up_item->seo_title = $filecontent->seo_title;
                    $up_item->seo_keywords = $filecontent->seo_keywords;
                    $up_item->seo_description = $filecontent->seo_description;
                    $up_item->seo_meta = $filecontent->seo_meta;

                    $up_item->save();

                    $file_history->file_id = $up_item->id;
                    $file_history->save();
                    return ResponseDto::createResObj(trans("admin/message.update_success"),ResponseDto::$SUCCESS);
                }
            }
            else{
                //添加操作
                $find = FileContent::where('name', $filecontent->name)->first();
                if($find){
                    return ResponseDto::createResObj(trans("admin/message.name_exit"),ResponseDto::$FAIL);
                }
                $filecontent->save();

                $file_history->file_id = $filecontent->id;
                $file_history->save();
                return ResponseDto::createResObj(trans("admin/message.submit_success"),ResponseDto::$SUCCESS);
            }




        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        $id = $request->input('id');
        //
        $view = View::make('admin.sys.filecontents_edit')
            ->withUrltitle(trans("admin/message.fileedit_index"))
            ->withAssets($request['ASSETS']);


        if($id){
            $find = FileContent::find($id);
            if($find){
                if($find->parent_id){
                    $parent = FileContent::select("name")->where("id",$find->parent_id)->first();
                    if($parent){
                        $find->parent_name = $parent->name;
                    }

                }
                $view-> withContent($find);
            }
        }
        return $view;
    }
    public function show($id)
    {

        if($id){
            $find = FileContent::find($id);
            if($find){
                if($find->parent_id){
                    $parent = FileContent::select("name")->where("id",$find->parent_id)->first();
                    if($parent){
                        $find->parent_name = $parent->name;
                    }

                }
            }
        }
        return $find;
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //

        if($id != null && $id != ""){
            FileContent::destroy($id);
            FileContent::where('parent_id',$id)->delete();
        }


        return ResponseDto::createResObj(trans("admin/message.update_success"),ResponseDto::$SUCCESS);
    }


    public  function getContentHistory(){
        return json_encode(FileContentHistory::select('id','created_at','title')->where('file_id',request()->input("id"))->orderBy('created_at','desc')->get());
    }
    public  function getContentHistoryById(){
        return json_encode(FileContentHistory::find(request()->input("id")));
    }
}
